<?php
$sqlCon=NULL;
if(!function_exists('sqlConnect'))
{ // check if function already defined to avoid the (function redeclared error)

    /**
     * automated function to connect to mysql database that takes default arguments to save the programmer
     * having to refill parameters on every call
     * @version 1.1 (stable)
     * @author Ronin
     * @param string $sServer [optional] mysql server address
     * @param string $sServerUser [optional] mysql server user name
     * @param string $sServerPass [optional] mysql server pass word
     * @return void
     */
    function sqlConnect($sServer="localhost",$sServerUser="root",$sServerPass="")
    {
        global $sqlCon;
        if($sqlCon == NULL || !mysql_ping($sqlCon))
        {
            $sqlCon=mysql_connect($sServer,$sServerUser,$sServerPass,true) or die(mysql_error());
            mysql_select_db("mydb",$sqlCon) or die(mysql_error());
        }
    }
}

if(!function_exists("sqlClose"))
{
    /**
     * Closes the global connection object
     * @author Ronin
     * @version 1.0 (untested)
     * @global null $sqlCon
     */
    function sqlClose()
    {
        global $sqlCon;
        mysql_close($sqlCon);
        $sqlCon=NULL;
    }
}

if(!function_exists("iInsert"))
{  // check if function already defined to avoid the (function redeclared error)
    /**
     * Inserts a row into the database avoiding sql injection security risks, the function also connects to mysql server
     * automatically so no need to establish a connection first
     * @author Ronin
     * @version 1.1 (stable)
     * @param string $sTable name of table to insert the record into
     * @param associative_array $arrValues an associative array with the following format.<br>
     * keys: the column names to insert in the table<br>
     * Values: values for each column to insert<br>
     * @example insertRow("comment",array('commentor_id'=>1,'span_id'=>5,'version_id'=>1,'text'=>'hello world'));
     * @return int,string function returns 1  indicating success or a string containing the query that was sent to
     * the database indicating failure, the returned string is for debugging purposes
     */
    function iInsert($sTable,$arrValues)
    {
        if(!is_string($sTable))
        {
            trigger_error("Function 'insertRow' expects '\$sTable' to be a string",E_USER_ERROR);
            return;
        }
        if(!is_array($arrValues))
        {
            trigger_error("Function 'insertRow' expects '\$arrValues' to be an array",E_USER_ERROR);
            return;
        }
        global $sqlCon;
        sqlConnect();
        $sQuery="INSERT INTO " . $sTable . " (";
        $arrKeys=array_keys($arrValues);
        $sQuery .= implode(",",$arrKeys) . ")" . " VALUES (";
        $len=count($arrKeys);
        $sFirst=mysql_real_escape_string($arrValues[$arrKeys[0]],$sqlCon);
        if(strpos($sFirst,"E__") === 0)
            $sQuery.=substr($sFirst,3);
        else
            $sQuery.="'" . $sFirst . "'";
        for($i=1; $i < $len; $i++)
        {
            $sElement=mysql_real_escape_string($arrValues[$arrKeys[$i]],$sqlCon);
            if(strpos($sElement,"E__") === 0)
                $sQuery.="," . substr($sElement,3);
            else
                $sQuery.=",'" . $sElement . "'";
        }
        $sQuery.=")";
        if(mysql_query($sQuery,$sqlCon))
            return 1;
        else
            return $sQuery . "\n" . mysql_error();
    }
}
if(!function_exists("rsSelect"))
{ // check if function already defined to avoid the (function redeclared error)

    /**
     *  This function acts as an alias for a mysql select statement, it takes care
     * of establishing a connection to the mysql server and securing the input against sql injection attacks before executing the query as
     * well as checking if the resulting resource is valid before returning it
     * @author Ronin
     * @version 1.2 (stable)
     * @param string|array $arrCols name of the column or the array of columns to select
     * @param string|array $arrTables [optional] name of the table or the array of tables to select from
     * @param array $arrConditions [optional] an array of WHERE clause conditions in the following format:<br>
     * {'condition along with operator',value,'if more conditions exist then a bitwise operator if a certain ordering is required
     * then the complete order statement else no element is necessary'}
     * @param string $sJoinConditions [optional] [untested] a string representing any special join statements like inner join, left join or outer join
     * including the table names to be joined, if this parameter is used then the user has to pass $arrConditions as well even if there are no
     * WHERE clause conditions
     * @param boolean [optional] states whether to select distinct values or not
     * @example simple:<br> $resultset=rsSelect("comment_id","comment",array("comment_id=",1))<br>
     * complex:<br>   $resultset=rsSelect(array('m.member_id','m.first_name'),
     * array('member m','comment c'),
     * array('c.commentor_id=','E__m.member_id','AND','m.email=','lostsamurai@ovi.com','ORDER BY m.member_id DESC'),
     * "Inner join m,c")
     * The previous complex example demostrates the use of the prefix 'E__' which marks the string for escaping
     * since m.member_id is not a variable name or a plain string and should be regarded as a column name, the use of the prefix 'E__'
     * tells the function not to add quotations around this variable before sending the query to mysql server
     * @return resultset|string in case the function was successful a resource resultset is returned otherwise a string
     * is returned containing the query that was sent to the database along with the mysql_error() output
     */

    function rsSelect($arrCols,$arrTables=NULL,$arrConditions=NULL,$sJoinConditions=NULL)
    {
        if($arrConditions !== NULL && !is_array($arrConditions))
        {
            trigger_error("Function select expects '\$arrConditions' to be an array",E_USER_ERROR);
            return;
        }
        if(!is_array($arrCols) && !is_string($arrCols))
        {
            trigger_error("Function select expects '\$arrCols' to be either an array or a string",E_USER_ERROR);
            return;
        }
        if($arrTables !== NULL && !is_array($arrTables) && !is_string($arrTables))
        {
            trigger_error("Function select expects '\$arrTables' to be either an array or a string",E_USER_ERROR);
            return;
        }
        if($sJoinConditions !== NULL && !is_string($sJoinConditions))
        {
            trigger_error("Function select expects '\$JoinConditions' to be a string",E_USER_ERROR);
            return;
        }
        global $sqlCon;
        sqlConnect();
        $sQuery="SELECT ";
        if(is_string($arrCols))
        {
            $sQuery.=$arrCols;
        }
        else
        {
            $sQuery.=implode(",",$arrCols);
        }

        if($arrTables !== NULL)
        {
            $sQuery.=" FROM ";
            if(is_string($arrTables))
            {
                $sQuery.= $arrTables . " ";
            }
            else
            {
                $sQuery.=implode(",",$arrTables) . " ";
            }
        }
        if($arrConditions !== NULL)
        {
            $sQuery.="WHERE ";
            $len=count($arrConditions);
            for($i=0; $i < $len; $i++)
            {
                $sQuery.= $arrConditions[$i];
                $i++;
                if(strpos($arrConditions[$i],"E__") === 0)
                {
                    $sQuery.= mysql_real_escape_string(substr($arrConditions[$i],3),$sqlCon) . " ";
                }
                else
                {
                    $sQuery.="'" . mysql_real_escape_string($arrConditions[$i],$sqlCon) . "' ";
                }
                $i++;
                if($i >= $len)
                    break;
                else
                    $sQuery.=$arrConditions[$i] . " ";
            }
        }

        if($sJoinConditions !== NULL)
        {
            $sQuery.=$sJoinConditions;
        }
        $rsResult=mysql_query($sQuery,$sqlCon);
        if(!$rsResult)
            return $sQuery . "\n" . mysql_error();
        else
            return $rsResult;
    }
}

if(!function_exists("iUpdate"))
{

    /**
     * This function acts as an alias for a mysql update statement, it takes care
     * of establishing a connection to the mysql server and securing the input against sql injection attacks before executing the query as
     * @author Ronin
     * @version 1.0 (alpha) needs more testing
     * @param string $sTable name of the table to update
     * @param associative_array $arrSet [optional] an associative array containing the columns to update along with the update value in the following format<br>
     * key: column to update
     * value: the new value
     * @param array $arrConditions an array of WHERE clause conditions to apply in the following format:<br>
     * {'condition along with operator',value,'if more conditions exist then a logical operator else no element is necessary'}
     * @return int,string the function returns 1 on success but on failure it shows the mysql_error() along with
     * the actual query that was sent to mysql server for debugging purposes
     */
    function iUpdate($sTable,$arrSet,$arrConditions=NULL)
    {
        if(!is_string($sTable))
        {
            trigger_error("Function iUpdate expects parameter '\$sTable' to be string",E_USER_ERROR);
            return;
        }
        if(!is_string($arrSet) && !is_array($arrSet))
        {
            trigger_error("Function iUpdate expects parameter '\$arrSet' to be string or array",E_USER_ERROR);
            return;
        }
        if($arrConditions !== NULL && !is_array($arrConditions))
        {
            trigger_error("Function iUpdate expects parameter '\$arrConditions' to be array",E_USER_ERROR);
            return;
        }
        global $sqlCon;
        sqlConnect();
        $sQuery="UPDATE " . $sTable . " SET ";
        $arrkeys=array_keys($arrSet);
        $len=count($arrkeys);
        $sQuery.=$arrkeys[0] . "='" . mysql_real_escape_string($arrSet[$arrkeys[0]],$sqlCon) . "'";
        for($i=1; $i < $len; $i++)
        {
            $sQuery.="," . $arrkeys[$i] . "='" . mysql_real_escape_string($arrSet[$arrkeys[$i]],$sqlCon) . "'";
        }
        if($arrConditions !== NULL)
        {
            $sQuery.=" WHERE ";
            $len=count($arrConditions);
            for($i=0; $i < $len; $i++)
            {
                $sQuery.=$arrConditions[$i];
                $i++;
                if(strpos($arrConditions[$i],"E__") === 0)
                {
                    $sQuery.=mysql_real_escape_string(substr($arrConditions[$i],3),$sqlCon) . " ";
                }
                else
                {
                    $sQuery.="'" . mysql_real_escape_string($arrConditions[$i],$sqlCon) . "' ";
                }
                $i++;
                if($i >= $len)
                    break;
                else
                {
                    $sQuery.=$arrConditions[$i] . " ";
                }
            }
        }
        if(!mysql_query($sQuery,$sqlCon))
            return $sQuery . "\n" . mysql_error();
        else
            return 1;
    }
}
if(!function_exists("iDelete"))
{
    /**
     * This function is an alias to a mysql DELETE statement,it takes care of establishing the connection
     * and handling the input to be secure against sql injection attacks
     * @author Ronin
     * @version 1.0
     * @param string $sTable name of the table to delete from
     * @param array $arrConditions an array of conditions for the to be deleted rows
     * @return int|string the function returns 1 if successful or a string indicating failure of one or more
     * of the internal queries for debugging purposes
     */
    function iDelete($sTable,$arrConditions)
    {
        if(!is_string($sTable))
        {
            trigger_error("Function iDelete expects parameter '\$sTable' to be string",E_USER_ERROR);
            return;
        }
        if(!is_array($arrConditions))
        {
            trigger_error("Function iDelete expects parameter '\$arrConditions' to be an array",E_USER_ERROR);
            return;
        }
        global $sqlCon;
        sqlConnect();
        $sQuery="DELETE FROM " . $sTable . " WHERE ";
        $len=count($arrConditions);
        for($i=0; $i < $len; $i++)
        {
            $sQuery.=$arrConditions[$i];
            $i++;
            if(strpos($arrConditions[$i],"E__") === 0)
            {
                $sQuery.=mysql_real_escape_string(substr($arrConditions[$i],3),$sqlCon) . " ";
            }
            else
            {
                $sQuery.="'" . mysql_real_escape_string($arrConditions[$i],$sqlCon) . "' ";
            }
            $i++;
            if($i >= $len)
                break;
            else
            {
                $sQuery.=$arrConditions[$i] . " ";
            }
        }
        if(!mysql_query($sQuery,$sqlCon))
            return $sQuery . "\n" . mysql_error();
        else
            return 1;
    }
}
if(!function_exists("sEscape"))
{
    /**
     * This function is used to escape any database output before sending
     * it to the user
     * @author Ronin
     * @version 1.0
     * @param string $sOutput the string to be escaped
     * @return string the escaped string
     */
    function sEscape($sOutput)
    {
        return htmlentities($sOutput,ENT_QUOTES,"UTF-8");
    }
}
if(!function_exists("iRoleId"))
{
    /**
     * Retrieves the id of the specified role
     * @author Ronin
     * @version 1.0
     * @param string $sRole the name of the role to retrieve the id for
     * @return int|string the function returns the id of the supplied role as an integer or returns a string
     * if any of the internal queries fail for debugging purposes
     */
    function iRoleId($sRole)
    {
        $rsRole=rsSelect("privileges_id","privileges",array("role=",$sRole));
        if(is_string($rsRole))
            return $rsRole;
        else
        {
            if(mysql_num_rows($rsRole) == 0)
                return $sRole . " does not exist in privileges";
            $row=mysql_fetch_assoc($rsRole);
            $iId=$row['privileges_id'];
            return (int)$iId;
        }
    }
}

if(!function_exists("bAuthorAll"))
{
    /**
     * The function checks if the member is an author in the supplied conference but unlike bAuthor it does not
     * check that he is an author of a specific paper
     * @author Ronin
     * @version 1.0
     * @param int $member_id member id of the member to check for
     * @param int $conference_id conference id to check against
     * @return boolean|string returns a boolean value indicating if the member is an author in the conference
     * or a string for debugging purposes if any of the internal queries fail
     */
    function bAuthorAll($member_id,$conference_id)
    {
        $iAuthorId=iRoleId("author");
        if(is_string($iAuthorId))
            return $iAuthorId;
        else
        {
            $rsAuthor=rsSelect("*","member_privileges",array("member_id=",$member_id,"AND","privileges_id=",$iAuthorId,"AND","conference_id=",$conference_id));
            if(is_string($rsAuthor))
                return $rsAuthor;
            else
            {
                if(mysql_num_rows($rsAuthor) == 0)
                    return false;
                else
                    return true;
            }
        }
    }
}
if(!function_exists("bAuthor"))
{

    /**
     * This function checks if a certain member is the author of a paper by using the version
     * id to acquire the paper id and then test to see if the supplied member is the author for
     * it
     * @author Ronin
     * @version 1.0
     * @param int $iMemberId   id of the member to test against
     * @param int $iVersionId the version id of the paper to check
     * @return boolean,string the function returns a boolean value indicating if the member is indeed the author or not or a string value indicating
     * the failure of one of the internal queries
     */
    function bAuthor($iMemberId,$iVersionId)
    {
        $rsPaper=rsSelect("paper_id","version",array("version_id=",$iVersionId));
        if(!is_string($rsPaper))
        {
            $row=mysql_fetch_assoc($rsPaper);
            $paper_id=$row['paper_id'];
            $rsAuthor=rsSelect("author_id","paper",array("paper_id=",$paper_id));
            if(!is_string($rsAuthor))
            {
                $row=mysql_fetch_assoc($rsAuthor);
                $author_id=$row['author_id'];
                if($author_id == $iMemberId)
                    return true;
                else
                    return false;
            }
            else
                return $rsAuthor;
        }
        else
            return $rsPaper;
    }
}

if(!function_exists("bHasRole"))
{
    /**
     * This function checks if the supplied member has the supplied role in the supplied conference
     * @author Ronin
     * @version 1.0.1 (beta)
     * @param int $iMemberId Member id to test for
     * @param int $iConferenceId conference id to test against
     * @param string $sRole the string representation of the role to check for as laid out in the database
     * @return boolean|string the function returns a boolean value indicating if the member has the given role in the
     * given conference or not, or a string value if any of the internal queries fail for debugging purposes
     */
    function bHasRole($iMemberId,$iConferenceId,$sRole)
    {
        $iRole=iRoleId($sRole);
        if(is_string($iRole))
            return $iRole;
        else
        {
            $rsRole=rsSelect("*","member_privileges",array("member_id=",$iMemberId,"AND","privileges_id=",$iRole,"AND","conference_id=",$iConferenceId));
            if(is_string($rsRole))
                return $rsRole;
            else
            {
                if(mysql_num_rows($rsRole) > 0)
                    return true;
                else
                    return false;
            }
        }
    }
}
if(!function_exists("bOrganizer"))
{
    /**
     * This function checks if the supplied member is registered as an organizer in the supllied conference
     * @author Ronin
     * @version 1.0 (alpha)
     * @param int $iMemberId the id of the member to test for
     * @param int $iConferenceId the id of the conference to test against
     * @return boolean,string the function returns a boolean value indicating if the member is an organizer in the supplied conference or not
     * or a string if any of the internal queries fail
     */
    function bOrganizer($iMemberId,$iConferenceId)
    {
        $iOrganizer=iRoleId("organizer");
        if(is_string($iOrganizer))
            return $iOrganizer;
        else
        {
            $rsOrganizer=rsSelect("*","member_privileges",array("member_id=",$iMemberId,"AND","privileges_id=",$iOrganizer,"AND","conference_id=",$iConferenceId));
            if(is_string($rsOrganizer))
                return $rsOrganizer;
            else
            {
                if(mysql_num_rows($rsOrganizer) > 0)
                    return true;
                else
                    return false;
            }
        }
    }
}

if(!function_exists("bReviewerAll"))
{
    /**
     * The function checks if the member is a reviewer in the supplied conference but unlike bReviewer it does not
     * check that he is assigned to a specific paper
     * @author Ronin
     * @version 1.0
     * @param int $member_id member id of the member to check for
     * @param int $conference_id conference id to check against
     * @return boolean|string returns a boolean value indicating if the member is a reviewer in the conference
     * or a string for debugging purposes if any of the internal queries fail
     */
    function bReviewerAll($iMemberId,$iConferenceId)
    {
        $iReviewerId=iRoleId("reviewer");
        if(is_string($iReviewerId))
            return $iReviewerId;
        else
        {
            $rsReviewer=rsSelect("*","member_privileges",array("member_id=",$iMemberId,"AND","privileges_id=",$iReviewerId,"AND","conference_id=",$iConferenceId));
            if(is_string($rsReviewer))
                return $rsReviewer;
            else
            {
                if(mysql_num_rows($rsReviewer) == 0)
                    return false;
                else
                    return true;
            }
        }
    }
}
if(!function_exists("bReviewer"))
{
    /**
     * The function checks if the supplied member is assiged to the supplied paper in the supplied conference
     * @author Ronin
     * @version 1.0 (alpha)
     * @param int $member_id id of the member to test for
     * @param int $conference_id id of the conference to test the member is assigned to
     * @param int $paper_id id of the paper that is registered under the supplied conference to test if the user is assigned to it
     * @return boolean|string the function returns a boolean value indicating if the supplied member is indeed assigned to the conference and the paper given in the
     * function arguments or a string if any of the internal queries fail
     */
    function bReviewer($member_id,$conference_id,$paper_id)
    {
        $rsReviewer=rsSelect("*","assigned_papers",array("member_id=",$member_id,"AND","conference_id=",$conference_id,"AND","paper_id=",$paper_id));
        if(is_string($rsReviewer))
            return $rsReviewer;
        else
        {
            if(mysql_num_rows($rsReviewer) > 0)
                return true;
            else
                return false;
        }
    }
}
if(!function_exists("iCategoryId"))
{
    /**
     * This function returns the id of the give category if it exists in the database
     * @author Ronin
     * @version 1.0 (alpha)
     * @param string $sCategoryName name of the category to get the id for
     * @return int|string the function returns the id number of the give criteria or a string if any of the internal
     * queries fail
     */
    function iCategoryId($sCategoryName)
    {
        $rsCategoryId=rsSelect("category_id","category",array("category=",$sCategoryName));
        if(is_string($rsCategoryId))
            return $rsCategoryId;
        else
        {
            if(mysql_num_rows($rsCategoryId) == 0)
                return "Category not found";
            else
            {
                $row=mysql_fetch_assoc($rsCategoryId);
                return (int)$row['category_id'];
            }
        }
    }
}

if(!function_exists("sCategoryName"))
{
    function sCategoryName($iCategoryId)
    {
        $rsCategoryName=rsSelect("category","category",array("category_id=",$iCategoryId));
        if(is_string($rsCategoryName))
            return $rsCategoryName;
        else
        {
            if(mysql_num_rows($rsCategoryName) == 0)
                return "Category not found";
            else
            {
                $row=mysql_fetch_assoc($rsCategoryName);
                return $row['category'];
            }
        }
    }
}

if(!function_exists("bSubscribed"))
{
    /**
     * checks if a member is subscribed in a conference
     * @author Ronin
     * @version 1.0 (stable)
     * @param int $iMemberId
     * @param int $iConferenceId
     * @return boolean|string function returns a boolean value indicating if the member is subscribed to the conference or a string value for debugging if
     * any of the internal queries fail
     */
    function bSubscribed($iMemberId,$iConferenceId)
    {
        $rsSubscribed=rsSelect("*","member_privileges",array("member_id=",$iMemberId,"AND","conference_id=",$iConferenceId));
        if(is_string($rsSubscribed))
            return $rsSubscribed;
        else
        {
            if(mysql_num_rows($rsSubscribed) > 0)
                return true;
            else
                return false;
        }
    }
}

if(!function_exists("bPending"))
{
    function bPending($iMemberId,$iConferenceId,$sType)
    {
        $iType=iRoleId($sType);
        if(is_string($iType))
            return $iType;
        else
        {
            $rsPending=rsSelect("*","member_subscribesto_conference",array("member_id=",$iMemberId,"AND","conference_id=",$iConferenceId,"AND","request_type=",$iType,"AND","approve ","E__IS NULL"));
            if(is_string($rsPending))
                return $rsPending;
            else
            {
                if(mysql_num_rows($rsPending) == 0)
                    return false;
                else
                    return true;
            }
        }
    }
}

if(!function_exists("iLastInsert"))
{
    function iLastInsert()
    {
        global $sqlCon;
        $rsLastId=rsSelect("LAST_INSERT_ID()");
        if(is_string($rsLastId))
            return $rsLastId;
        else
        {
            $row=mysql_fetch_assoc($rsLastId);
            return $row['LAST_INSERT_ID()'];
        }
    }
}
if(!function_exists("sHashNewPassword"))
{
    function sHashNewPassword($sEmail,$sPassword)
    {
        $salt=hash("sha256",uniqid(mt_rand(),true) . strtolower($sEmail));
        $password=$salt . $sPassword;
        for($i=0; $i < 10000; $i++)
        {
            $password=hash("sha256",$password);
        }
        $password=$salt . $password;
        return $password;
    }
}
if(!function_exists("bCheckPassword"))
{
    function bCheckPassword($sEmail,$sPassword)
    {
        $rsPassword=rsSelect("password","member",array("email=",$sEmail));
        if(is_string($rsPassword))
            return $rsPassword;
        if(mysql_num_rows($rsPassword) == 0)
            return false;

        $row=mysql_fetch_assoc($rsPassword);
        $salt=substr($row['password'],0,64);
        $password=$salt . $sPassword;
        for($i=0; $i < 10000; $i++)
        {
            $password=hash("sha256",$password);
        }
        $password=$salt . $password;
        if($password == $row['password'])
            return true;
        else
            return false;
    }
}

if(!function_exists("sOperatingSystem"))
{
    /**
     * Gets the underlying operating system as a string currently supporting linux and windows only
     * @author Ronin
     * @version 1.0 (alpha)
     * @return string the string representation of the underlying operating system name e.g linux or windows
     */
    function sOperatingSystem()
    {
        if(strpos(strtolower(php_uname()),"linux") !== false)
            return "linux";
        else if(strpos(strtolower(php_uname()),"winnt") !== false || strpos(strtolower(php_uname()),"windows") !== false || strpos(strtolower(php_uname()),"win32") !== false)
            return "windows";
    }
}
if(!function_exists("iReformat"))
{

    /**
     * reformats newly converted html files to exploit every possible pattern used by
     * pdftohtml converter to increase efficiency
     * @author Ronin
     * @version 1.0.7 (beta)
     * @param int $version_id id of the version to reformat
     * @return int the function returns -1 if the database did not contain any versions
     * with the provided $version_id and 1 if the process was completed successfully
     */
    function iReformat($version_id)
    {
        global $sqlCon;
        sqlConnect();
        $rsResult=rsSelect("path","version",array('version_id=',$version_id));
        if(mysql_num_rows($rsResult) == 0)
            return -1;
        else
        {
            $row=mysql_fetch_assoc($rsResult);
            $sFilePath=$row['path'];
            $arrInfo=pathinfo($sFilePath);
            $sFilePath=$arrInfo['dirname'] . "/" . $arrInfo['filename'] . ".html";
            $sHtml=file_get_contents($sFilePath);
            if($sHtml == false)
                return -1;
            $iCount=1;
            //if(sOperatingSystem() == "windows")
            {
                $iSpans=substr_count($sHtml,'<span ');
                $arrIds=array();
                $arrSpans=array();
                for($i=0; $i < $iSpans; $i++)
                {
                    $iCurr=$i + 1;
                    $arrIds[$i]='<span id="' . $iCurr . '" class';
                    $arrSpans[$i]='/<span class/';
                }
                $sReplaced_SPANS=preg_replace($arrSpans,$arrIds,$sHtml,$iCount);
				if(preg_match("/.\/paper\//",$sHtml)===0)
				{
					$sReplaced_SPANS_PAPER=preg_replace("/src=\"/","src=\"./paper/",$sReplaced_SPANS);       
					$fpHandle=fopen($sFilePath,"w");
					fwrite($fpHandle,$sReplaced_SPANS_PAPER);
					fflush($fpHandle);
					fclose($fpHandle);
				}
				else
				{     
					$fpHandle=fopen($sFilePath,"w");
					fwrite($fpHandle,$sReplaced_SPANS);
					fflush($fpHandle);
					fclose($fpHandle);
				}
                return 1;
            }
            /*else if(sOperatingSystem() == "linux")
            {
                $iPs=substr_count($sHtml,"<P ");
                $arrIds=array();
                $arrPs=array();
                for($i=0; $i < $iPs; $i++)
                {
                    $iCurr=$i + 1;
                    $arrIds[$i]='<P id="' . $iCurr . '" style';
                    $arrPs[$i]='/<P style/';
                }
                $sReplaced_P=preg_replace($arrPs,$arrIds,$sHtml,$iCount);
                $sReplaced_P_PAPER=preg_replace("/src=\"/","src=\"./paper/",$sReplaced_P);
                $fpHandle=fopen($sFilePath,"w");
                fwrite($fpHandle,$sReplaced_P_PAPER);
                fflush($fpHandle);
                fclose($fpHandle);
                return 1;
            }*/
        }
    }
}
?>
